package com.example.hp.yuejian5.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.example.hp.yuejian5.R;
import com.example.hp.yuejian5.RetrofitListener.RetrofitListener;
import com.example.hp.yuejian5.adapter.ActivityAdapter;
import com.example.hp.yuejian5.bean.ActivityBean;
import com.example.hp.yuejian5.model.ActivityModel;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ActivityFragment extends Fragment {

    private RecyclerView recyclerView;
    private List<ActivityBean> list=new ArrayList<ActivityBean>();
    private ActivityAdapter activityAdapter;
    private RecyclerView.LayoutManager layoutManager;
    public RetrofitListener<List<ActivityBean>> listener;
    public ActivityFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        getactivitylist();
    }
    private void initView(View v)
    {
        recyclerView=v.findViewById(R.id.activity_recyclerview);
        layoutManager=new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        activityAdapter=new ActivityAdapter(getActivity());
        recyclerView.setAdapter(activityAdapter);
    }

    private void getactivitylist()
    {
        ActivityModel activityModel=new ActivityModel();
        activityModel.activityList(listener);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        listener=new RetrofitListener<List<ActivityBean>>() {
            @Override
            public void onSuccess(List<ActivityBean> list, int flag) {
                activityAdapter.activityData(list);
            }

            @Override
            public void onFail() {
                Toast.makeText(getActivity(),"获取失败",Toast.LENGTH_SHORT).show();
            }
        };
        return inflater.inflate(R.layout.fragment_activity, container, false);
    }

}
